/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ASFont;
import org.aswing.JButton;

import fever.app.accelerator.Keyboard;
import fever.app.accelerator.KeyDefinition;
import fever.app.accelerator.ShortcutLocator;
import fever.app.accelerator.ShortcutMap;
import fever.app.local.Localisation;
import fever.core.Resources;
import fever.log.FeverDebug;
import fever.medias.sound.AppSoundFactory;
import fever.structures.ShortcutString;
import fever.utils.Stringifier;

import fvaswing.FvAsWing;
import fvaswing.structure.FvFont;

/**
 * {@code FvButton } class adds <strong>Fever</strong> features to basic 
 * JButton class.
 * 
 * <ul>
 *   <li>Sound support using {@link #setSoundID()} method</li>
 *   <li>Shortcut support using {@link #setShortcut()} method</li> *   <li>Fever Localisation API support using {@link #setLocalisationID()} method</li>
 * </ul>
 * 
 * {@code
 *   var button : FvButton = new FvButton( 'click me please' );
 *   button.setSoundID( 'click' );
 *   button.setShortcut( new KeyCombo( Keyboard.onKeyCONTROL, Keyboard.onKeyO ), 'myKeyMap' );
 *   button.setToolTipText( 'open file' );
 *   button.addActionListener( _onClick, this );
 * }
 * 
 * <p>You can use auto label shortcut definition too : ( passing {@code null} as
 * {@code key} argument :
 * {@code
 *   var button : FvButton = new FvButton( 'click &me please' );
 *   button.setShortcut( null, 'myKeyMap' );
 *   button.setToolTipText( 'open file' );
 *   button.addActionListener( _onClick, this );
 * }
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.FvButton extends JButton
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _soundID : String;
	private var _keymapName : String;
	private var _keyDef : KeyDefinition;
	private var _sString : ShortcutString;
	private var _storeTip : String;
	private var font : FvFont; //override super class type
	private var _localNodeID : String;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** 
	 * Defines default sound id to search in current 
	 * {@link fever.medias.sound.AppSoundFactory} sound lib.
	 * 
	 * <p>Sound is not automatically added to button behaviour, use 
	 * {@link #setSoundID()} method to declare it.
	 * 
	 * <p>Example
	 * {@code
	 *   var b : FvButton = new FvButton( 'simple button' );
	 *   b.setSoundID( FvButton.DEFAULT_CLICK_SOUND );
	 * }
	 */
	public static var DEFAULT_CLICK_SOUND : String = 'click';
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
     * FvButton( text : String, icon : Icon )<br>
     * FvButton( text : String )<br>
     * FvButton( icon : Icon )
	 */
	public function FvButton( caption, icon )
	{
		super( caption, icon );
	}
	
	/**
	 * Sets button label.
	 * 
	 * <p>Checks if passed-in {@code caption} contains {@link #SHORTCUT_CHAR}.<br />
	 * If {@code true} automatically build a new shortcut for button.
	 * 
	 * @param caption Button label
	 */
	public function setText( caption : String ) : Void
	{
		if( caption != text )
		{
			_sString = new ShortcutString( caption );
			
			_applyMarkerOnFont();
			
			if( _keyDef || _keymapName ) setShortcut( null, _keymapName );
			
			repaint();
			invalidate();
		}
	}
	
	/**
	 * Returns button label.<br />
	 * Without any shortcut marker ( if present )
	 */
	public function getText() : String
	{
		return _sString.text;	
	}
	
	/**
	 * Sets sound id to play when user click on button.
	 * 
	 * <p>Sounds id is searched in current 
	 * {@link fever.medias.sound.AppSoundFactory} factory.
	 */
	public function setSoundID( soundID : String ) : Void
	{
		if( soundID ) _soundID = soundID;
	}
	
	/**
	 * Returns sound id associated to button click.
	 */
	public function getSoundID() : String
	{
		return _soundID;	
	}
	
	/**
	 * Sets new key shortcut to button.
	 * 
	 * <p>Pass {@code key == null} to enable built-in label shortcut 
	 * definition.  
	 */
	public function setShortcut( key : KeyDefinition, keymapID : String ) : Void
	{
		var keyMap : ShortcutMap;
			
		if( ShortcutLocator.isRegistered( keymapID ) )
		{
			keyMap = ShortcutLocator.getMap( keymapID );
			_keymapName = keyMap.getName();
		}
		else
		{
			keyMap = ShortcutMap.getApplicationMap();
			_keymapName = null;
			
			FeverDebug.WARN( 'use application keymap for ' + getText() + ' button' );
		}
		
		if( key )
		{
			_applyShortcut( keyMap, key );
			
			if( _sString.enabled ) font.removeShortcutIndex();
		}
		else if( _sString.enabled )
		{
			_applyShortcut( keyMap, Keyboard.getCharStruct( _sString.getMarker() ) );
		}
		else
		{
			if( _keyDef ) releaseShortcut();
		}
		
	}
	
	/**
	 * Releases key shortcut associated to button.
	 */
	public function releaseShortcut() : Void
	{
		var keymap : ShortcutMap = ( _keymapName ) 
			? ShortcutLocator.getMap( _keymapName )
			: ShortcutMap.getApplicationMap();
		
		keymap.unregister( _keyDef );
		
		_keyDef = null;
		
		if( _storeTip ) setToolTipText();
	}
	
	/**
	 * Connects button to localisation API.
	 * 
	 * @param nodeID Full qualified node path to translation node in local file
	 * @param resource Custom resource where to find {@code nodeID} node
	 */
	public function setLocalisationID( nodeID : String, resource : Resources ) : Void
	{
		if( nodeID != _localNodeID )
		{
			if( resource || Localisation.hasResource( nodeID ) )
			{
				_localNodeID = nodeID;
				Localisation.connectInstance( this, setText, nodeID, resource );	
			}
			else Localisation.disconnectInstance( this );
		}
	}
	
	/**
	 * Returns translation node path.
	 */
	public function getLocalisationID() : String
	{
		return _localNodeID;
	}
	
	/**
	 * Registers the text to display in a tool tip. 
	 * 
	 * <p>Take care if a shortcut is defined.<br />
	 * If {@code true} add '( shortcut name )' at the end of tooltip.
	 */
	public function setToolTipText( caption : String ) : Void
	{
		_storeTip = caption;
		
		super.setToolTipText( _buildToolTipText() );
	}
	
	/**
	 * Returns tooltips without shortcut caption.
	 */
	public function getTooltipTextWithoutShortcut() : String
	{
		return _storeTip;	
	}
	
	/**
	 * Sets the text font for this component.<br>
	 * this method will cause a repaint and revalidate method call.<br>
	 * 
	 * @param newFont the font to set for this component.
	 */
	public function setFont( newFont : ASFont ) : Void
	{
		if( font != newFont )
		{
			font = FvFont.getFromAsFont( newFont );
			
			_applyMarkerOnFont();
		}
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _buildToolTipText() : String
	{
		if( _keyDef && !_sString.enabled )
		{
			return ( _storeTip + ' ( ' + _keyDef.getName() + ' )' );
		}
		
		return _storeTip;	
	}
	
	private function __onRelease() : Void
	{
		_checkSound();
		
		super.__onRelease();
	}
	
	private function _checkSound( source : FvButton ) : Void
	{
		if( _soundID ) AppSoundFactory.getSoundFactory().getSound( _soundID ).start();
	}
	
	private function _applyShortcut( keyMap : ShortcutMap, key : KeyDefinition ) : Void
	{
		if( _keyDef ) releaseShortcut();
		
		keyMap.registerCustomType( FvAsWing.AWSHORTCUT, key, this );
       
       _keyDef = key;
       
       if( _storeTip ) setToolTipText();	
	}
	
	private function _applyMarkerOnFont() : Void
	{
		if( _sString.enabled ) font.setShortcutIndex( _sString.index );
		else font.removeShortcutIndex();	
		
		setFontValidated( false );
		repaint();
		revalidate();
	}
	
}